import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Font;
import java.awt.Toolkit;
import java.util.Locale;

import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JEditorPane;

import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.SwingConstants;

@SuppressWarnings("unused")
public class Preguntas extends JDialog {
	private static final long serialVersionUID = 1L;
	private  JPanel contentPanel = new JPanel();
	private JLabel lblPregunta;
	private JLabel lblImagenAnimal;
	private JLabel lblImagemAkinator;
	private JButton btnSi;
	private JButton btnNo;
	private JLabel lblAnimalAdivinado;
	private JPanel buttonPane;
	private JButton btnJugarOtraVez;
	private JButton btnSalir;
	private JLabel lblFondo;
	/**
	 * Launch the application.
	 */	
	public static void main(String[] args) {
		try {
			Preguntas dialog = new Preguntas();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Preguntas() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Preguntas.class.getResource("/imagenes/Logo_UNGS-recorte.png")));
		setTitle("Akinator-Animal");
		setBounds(100, 100, 574, 371);
		getContentPane().setLayout(null);
		
		
		{
			buttonPane = new JPanel();
			buttonPane.setBounds(0, 298, 572, 38);
			buttonPane.setBackground(Color.WHITE);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane);
			{
				btnSi = new JButton("SI");
				btnSi.setForeground(Color.GREEN);
				btnSi.setFont(new Font("DejaVu Serif", Font.BOLD, 14));
				btnSi.setBackground(new Color(255, 255, 255));
				btnSi.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Inteligencia.setRespuestaJugador(true);
						Inteligencia.eliminarAnimalesNoElegidos();
						Preguntas.this.setVisible(false);
						lblPregunta.setText(Inteligencia.damePregunta());
						Preguntas.this.setVisible(true);
						mostrarAnimalElegido();
					}
				});
				btnSi.setActionCommand("OK");
				buttonPane.add(btnSi);
				getRootPane().setDefaultButton(btnSi);
			}
			{
				btnNo = new JButton("NO");
				btnNo.setForeground(Color.RED);
				btnNo.setFont(new Font("DejaVu Serif", Font.BOLD, 14));
				btnNo.setBackground(new Color(255, 255, 255));
				btnNo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Inteligencia.setRespuestaJugador(false);
						Inteligencia.eliminarAnimalesNoElegidos();
						Preguntas.this.setVisible(false);
						lblPregunta.setText(Inteligencia.damePregunta());
						Preguntas.this.setVisible(true);
						mostrarAnimalElegido();
					}
				});
				btnNo.setActionCommand("Cancel");
				buttonPane.add(btnNo);
			}
			{
				JButton btnCancelar = new JButton("Cancelar");
				btnCancelar.setFont(new Font("DejaVu Serif", Font.BOLD | Font.ITALIC, 12));
				btnCancelar.setBackground(Color.WHITE);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});
				buttonPane.add(btnCancelar);
			}
		}
		contentPanel.setBounds(0, 0, 569, 336);
		contentPanel.setBackground(Color.WHITE);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		
		lblImagemAkinator = new JLabel();
		lblImagemAkinator.setBounds(360, 0, 200, 271);
		lblImagemAkinator.setIcon(new ImageIcon(Preguntas.class.getResource("/imagenes/akinatorpensando200.png")));
		contentPanel.add(lblImagemAkinator);
		
		lblAnimalAdivinado = new JLabel("");
		lblAnimalAdivinado.setForeground(Color.WHITE);
		lblAnimalAdivinado.setFont(new Font("DejaVu Serif", Font.BOLD, 24));
		lblAnimalAdivinado.setHorizontalAlignment(SwingConstants.CENTER);
		lblAnimalAdivinado.setBounds(117, 225, 200, 29);
		contentPanel.add(lblAnimalAdivinado);
		
		lblImagenAnimal = new JLabel("");
		lblImagenAnimal.setBounds(155, 129, 156, 84);
		contentPanel.add(lblImagenAnimal);
		
		lblPregunta = new JLabel();
		lblPregunta.setHorizontalAlignment(SwingConstants.CENTER);
		lblPregunta.setForeground(Color.BLACK);
		lblPregunta.setEnabled(true);
		
		lblPregunta.setFont(new Font("DejaVu Serif", Font.BOLD, 15));
		lblPregunta.setBounds(28, 54, 388, 63);
		lblPregunta.setText(Inteligencia.damePregunta());
		contentPanel.add(lblPregunta);
		
		lblFondo = new JLabel("");
		lblFondo.setBounds(-20,-90, 625, 430);
		contentPanel.add(lblFondo);
		lblFondo.setIcon(new ImageIcon(Principal.class.getResource("/imagenes/desierto3.png")));
		
		
	}
	
	// Muestra por pantalla el animal elegido
	private void mostrarAnimalElegido(){
		if(Inteligencia.getCandidatos().size()==1){
			lblPregunta.setText("USTED PENSO EN ESTE ANIMAL:");
			lblImagenAnimal.setIcon(new ImageIcon(Preguntas.class.getResource
					(Inteligencia.dameImagenAnimal(Inteligencia.getCandidatos().get(0)))));
			lblImagemAkinator.setIcon(new ImageIcon(Preguntas.class.getResource
					("/imagenes/akinatorfinal.png")));
			lblAnimalAdivinado.setText((Inteligencia.getCandidatos().get(0)).toUpperCase());
			//buttonPane.setVisible(false);
			getContentPane().remove(buttonPane);
			
			btnJugarOtraVez = new JButton("Jugar Otra Vez");
			btnJugarOtraVez.setForeground(Color.GREEN);
			btnJugarOtraVez.setFont(new Font("DejaVu Serif", Font.BOLD, 14));
			btnJugarOtraVez.setBackground(new Color(255, 255, 255));
			btnJugarOtraVez.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Inteligencia.reset();
					Principal nuevo= new Principal();
					nuevo.getFramePrincipal().setVisible(true);
					// centrado respecto del padre
					nuevo.getFramePrincipal().setLocationRelativeTo(contentPanel);
					setVisible(false);
				}
			});
			btnJugarOtraVez.setBounds(120, 307, 169, 25);
			contentPanel.add(btnJugarOtraVez);
			
			btnSalir = new JButton("Salir");
			btnSalir.setForeground(Color.RED);
			btnSalir.setFont(new Font("DejaVu Serif", Font.BOLD, 14));
			btnSalir.setBackground(new Color(255, 255, 255));
			btnSalir.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					System.exit(0);
				}
			});
			btnSalir.setBounds(315, 307, 117, 25);
			contentPanel.add(btnSalir);
			
		}
	}
}
